package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.*;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysMySchedule;
import com.ccp.dev.system.service.SysMyScheduleService;
import com.ccp.dev.core.basic.annotion.Action;

import java.util.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 个人日程 信息操作处理控制器
 * 
 * @author zw
 * @date 2019-07-25 12:11:59
 */
@Controller
@Action(ownermodel = SysAuditModelType.USER_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_SYSTEM +"/sysMySchedule")
public class SysMyScheduleController extends SysBaseController {
	@Resource
	private SysMyScheduleService sysMyScheduleService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="个人日程首页页面跳转",exectype = "页面跳转日志")
    public String list() {
        return "sysMySchedule/list";
    }

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/calendar")
    @Action(description="日程日历页页面跳转",exectype = "页面跳转日志")
    public String calendar() {
        return "sysMySchedule/calendar";
    }



    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="日程新增页面页面跳转",exectype = "页面跳转日志")
    public String add(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        return "sysMySchedule/add";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: sysMySchedule 保存的对象
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description = "保存个人日程数据",
            detail = "<#if StringUtil.isEmpty(scheduleId)>添加<#else>更新</#if>日程【${scheduleName}】<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    public Object save(@RequestBody SysMySchedule sysMySchedule) {
            String id = sysMySchedule.getId();
            SysAuditThreadLocalHolder.putParamerter("scheduleId", id);
            SysAuditThreadLocalHolder.putParamerter("scheduleName", sysMySchedule.getScheduleName());
        try{
            int result;
            if (StringUtil.isEmpty(id)) {
                sysMySchedule.setId(UUIDUtils.getUUIDFor32());
                sysMyScheduleService.addSchedule(sysMySchedule);
            } else {
                sysMyScheduleService.update(sysMySchedule);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("save.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("save.failure");
        }
    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"id"
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @Action(description = "根据主键ID删除个人日程数据",
            detail = "删除个人日程<#if StringUtil.isNotEmpty(id)><#list fullName as item>【${item}】</#list></#if>" +
                    "<#if success>成功<#else>失败</#if>",
            exectype = "删除日志")
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    public Object delete(@RequestParam("id") String id) {
        SysAuditThreadLocalHolder.putParamerter("id", id);
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("delete.failure.message");
        }
        String[] ids = id.split(",");
        SysAuditThreadLocalHolder.putParamerter("fullName", sysMyScheduleService.queryScheduleNameScheduleIds(Arrays.asList(ids)));
        try{
            sysMyScheduleService.delByIdArray(ids);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("delete.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
    }
    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"id"
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @Action(description = "根据主键id查询个人日程的一条数据",
            detail = "查询【${schName}】详情<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    public Object get(@RequestParam("id") String id) {
        SysMySchedule sysMySchedule = sysMyScheduleService.getOneById(id);
        SysAuditThreadLocalHolder.putParamerter("schName", BeanUtils.isEmpty(sysMySchedule)?"":sysMySchedule.getScheduleName());
        if (null == sysMySchedule) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("get.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("get.success",sysMySchedule);
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-25 12:11:59
     */
    @Action(description = "附带检索条件的分页查询个人日程", exectype = "查询日志")
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, false);
        List<SysMySchedule> pageDatas = sysMyScheduleService.queryList(queryFilter);
        return success(pageDatas, pageDatas.size());
    }



    /**
     * 日历及layuigrid数据查询
     * @param request
     * @return
     */
    @Action(description = "日历及layuigrid数据查询", exectype = "查询日志")
    @RequestMapping(value = "/queryschedule")
    public @ResponseBody
    ResultData queryScheduleList(HttpServletRequest request) {
        try {
            Map<String, Object> queryMap = new HashMap<>(16);
            String startDate = RequestUtil.getString(request,"startDate");
            String endDate = RequestUtil.getString(request,"endDate");
            String scheduleType = RequestUtil.getString(request,"scheduleType");
            String scheduleUrgency = RequestUtil.getString(request,"scheduleUrgency");
            String scheduleComplete = RequestUtil.getString(request,"scheduleComplete");
            String viewType = RequestUtil.getString(request,"viewType");
            queryMap.put("scheduleUserID", ContextUtil.getCurrentUserId());

            if(StringUtil.isNotEmpty(scheduleType)){
                queryMap.put("scheduleType", Long.parseLong(scheduleType));
            }
            if(StringUtil.isNotEmpty(scheduleUrgency)){
                queryMap.put("scheduleUrgency", Long.parseLong(scheduleUrgency));
            }
            if(StringUtil.isNotEmpty(scheduleComplete)){
                queryMap.put("scheduleComplete", Long.parseLong(scheduleComplete));
            }

            //如果是日视图，就把endTime改为当天
            if ("timeGridDay".equalsIgnoreCase(viewType)) {
                endDate = startDate;
            } else {
                //如果是其它视图，就把结束日期减少一天
                Date e = new Date(Long.valueOf(endDate));
                endDate = String.valueOf(DateUtil.addDay(e, -1).getTime());
            }
            List<SysMySchedule> result = sysMyScheduleService.queryMySchedule(queryMap, DateFormatUtil.formatDate( new Date(Long.parseLong(startDate))),DateFormatUtil.formatDate( new Date(Long.parseLong(endDate))));
            return getResultDataSuccess("operation.success",result.size(),result);
        }catch (Exception ex){
            ex.printStackTrace();
            return getResultDataError("operation.failure");
        }
    }

    @Action(description = "日历拖拽", exectype = "编辑日志")
    @RequestMapping(value = "dragOrResize", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> dragOrResize(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            String ID = RequestUtil.getString(request,"id");
            String startDate = RequestUtil.getString(request,"startDate");
            String endDate = RequestUtil.getString(request,"endDate");
            Boolean allDay = RequestUtil.getBoolean(request,"allDay");
            Date start = new Date(Long.parseLong(startDate));
            Date end ;
            if(endDate==null ||endDate.length()==0){
                if(allDay){
                    end = DateUtil.addHour(start,24);//如果结束时间为空且是全天日程，则默认为开始时间之后24小时
                }else{
                    end = DateUtil.addHour(start,1);//如果结束时间为空，则默认为开始时间之后一个小时
                }
            }else{
                end = new Date(Long.parseLong(endDate));
            }
            SysMySchedule mySchedule = new SysMySchedule();
            mySchedule.setId(ID);
            mySchedule.setScheduleStart(start);
            mySchedule.setScheduleEnd(end);
            mySchedule.setUpdateBy(ContextUtil.getCurrentUserId());
            mySchedule.setUpdateTime(new Date());
            sysMyScheduleService.update(mySchedule);
            result.put("success", true);
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("success", false);
        }
        return result;
    }


}
